<template>
	<view>
		<st-mc-framework :navTitle="ModuleTitle" @changeLevel="changeLevel"
			@AddModuleData_select='AddModuleData_select'>
			<template #workbench>
				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0 }" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="120">
					<template #BtnContent>
						<u-icon name="search" color="#fff" size="25px" @click="search"></u-icon>
					</template>
				</m-drag-button>

				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0}" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="60">
					<template #BtnContent>
						<u-icon name="scan" color="#fff" size="25px" @click="scan"></u-icon>
					</template>
				</m-drag-button>


			</template>

			<!-- 主内容 -->
			<template #content>
				<view class="content_box f-dc">
					<view class="table table_top_th">
						<table>
							<tr>
								<th width='15%'>检验编号</th>
								<th width='20%'>产品名称</th>
								<th width='10%'>工序</th>
								<th width='11%'>检验日期</th>
								<th width='9%'>检验数</th>
								<th width='9%'>合格数</th>
								<th width='9%'>不合格数</th>
								<th width='8%'>不良率</th>
								<th width='10%'>检验结果</th>
								<th width='10%'>是否审核</th>
							</tr>

						</table>
					</view>
					<z-paging ref="paging" v-model="contents" @query="queryList" :fixed="false" class="z_paging">
						<view class="table">

							<table style="table-layout: fixed;">

								<tbody>
									<tr v-for="(item,index) in contents" @click="planIDClick(item,index)"
										:class="{ hovered: hoveredIndex === index }">
										<td width='15%'>
											<view class="newlineDiv"><text>{{item.BillNO}}</text></view>
										</td>
										<td width='20%'>
											<view class="newlineDiv"><text>{{item.ProductName}}</text></view>
										</td>
										<td width='10%'>
											<view class="newlineDiv"><text>{{item.ProcedureName}}</text></view>
										</td>
										<td width='11%'>
											<view class="newlineDiv"><text>{{item.BillDate}}</text></view>
										</td>
										<td width='9%'>
											<view class="newlineDiv"><text>{{item.CkAmount}}</text></view>
										</td>
										<td width='9%'>
											<view class="newlineDiv"><text>{{item.OkAmount}}</text></view>
										</td>
										<td width='9%'>
											<view class="newlineDiv"><text>{{item.BadAmount}}</text></view>
										</td>
										<td width='8%'>
											<view class="newlineDiv"><text>{{item.BadRate}}</text><text
													v-show="item.BadRate">%</text> </view>
										</td>
										<td width='10%'>
											<view class="newlineDiv"><text>{{item.ResultName}}</text></view>
										</td>
										<td width='10%'>
											<view class="newlineDiv"><text>{{item.Approve?'已审核':'未审核'}}</text></view>
										</td>

									</tr>
								</tbody>
							</table>

						</view>

					</z-paging>

				</view>
				<!-- 
				<view class="add">
					<u-icon name="scan" color="#fff" size="30" @click="scan"></u-icon>
				</view>
				<view class="search">
					<u-icon name="search" color="#fff" size="30" @click="search"></u-icon>
				</view> -->

				<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="10px 10px 0 0">
					<view class="popup_content">

						<u-cell-group :border='false'>
							<u-cell title="开始时间" :isLink="false" :value='FormData.FBDate'
								@click="addTime('开始')"></u-cell>
							<u-cell title="结束时间" :isLink="false" :value='FormData.FEDate'
								@click="addTime('结束')"></u-cell>
							<u-cell title="是否审核" :isLink="false" :value="FormData.Approve?'已审核':'未审核'"
								@click="addIsStart()"></u-cell>
						</u-cell-group>
						<view class="bottomBtns">
							<button block @click="submit">
								查 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 询
							</button>
						</view>

					</view>
				</uni-popup>

				<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show = false' @close="show=false"
					@confirm='pickerConfirm'></u-datetime-picker>
				<u-action-sheet :actions="IsStartList" :show="IsStartShow" @select="IsStartClick"
					@close="IsStartShow=false"></u-action-sheet>

			</template>

		</st-mc-framework>
		<u-modal :show="modalshow" :title="modaltitle" width="400" @confirm='modalconfirm'>
			<view class="slot-content">
				{{modalcontent}}
			</view>
		</u-modal>
		<m-loading :text="loadingName" v-model="showMLoading" :showClose="false"
			maskBackgroundColor="rgba(0,0,0,0.3)"></m-loading>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';

	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {
				ModuleTitle: '', //标题名称
				showMLoading: false, //是否加载
				loadingName: '正在加载中……', //加载名称
				modalshow: false, //提示显示
				modaltitle: '', //提示名称
				modalcontent: '', //提示内容

				FormData: {
					FWorkshopID: '1011',
					PageIndex: 1,
					PageSize: 30,
					FBDate: '',
					FEDate: '',
					FType: 'SJJ',
					Approve: 0,
					EquipmentID: null,
					EquipmentName: ''
				},
				contents: [], //列表
				show: false, //时间显示
				value1: Number(new Date()), //时间值
				TimeName: '', //时间名称
				hoveredIndex: null, //索引
				jobid: '',
				ProcedureList: [],
				IsStartShow: false,
				IsStartList: [{
						name: '未审核',
						id: 0
					},
					{
						name: '已审核',
						id: 1
					}
				],
				disabled: false,
				BtnShow: false, //按钮显示
			};
		},


		onReady() {
			var that = this
			uni.hideLoading();
			that.showMLoading = true
			const {
				appConfigInfo
			} = that.$store.state;

			that.ModuleTitle = appConfigInfo.ActiveModule.ModuleName

			that.$nextTick(() => {
				setTimeout(() => {
					that.BtnShow = true;
				}, 500)
			})
		},
		onLoad(options) {
			var that = this
			that.$safeStatus({
				payload: '其他', // 要传递的数据
			});
			const {
				GuyueModule,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;
			if (options.Dispatchitem) {
				const dispatchItem = JSON.parse(options.Dispatchitem);
				that.assignFormData(that.FormData, dispatchItem);
				return;
			}
			const formDefaults = {
				FWorkshopID: MesBasicInfo.FWorkshopID,
				FBDate: getBefore7DateTime(),
				FEDate: getFormatDateTime(new Date()),
				EquipmentID: MesBasicInfo.warehouse.EquipmentID,
				EquipmentName: MesBasicInfo.warehouse.EquipmentName,
			};
			that.assignFormData(that.FormData, formDefaults);
		},
		onShow() {
			var that = this
			this.clearHovered();
			console.log('啛啛喳喳');
			uni.$on('ChangeTabs', () => {
				console.log('啛啛喳喳');
				if (that.$refs.framework && typeof that.$refs.framework.ChangeTabsClick == 'function') {
					console.log('啛啛喳喳');
					that.$refs.framework.ChangeTabsClick()
				}
			});
			uni.$on('refreshList', () => {
				this.getList(); // 调用目标方法
			});
		},
		onUnload() {
			uni.$off('refreshList');
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		mounted() {

		},
		methods: {
			// 创建表单赋值工具函数
			assignFormData(target, defaults = {}) {
				var that = this;
				Object.keys(defaults).forEach(key => {
					target[key] = defaults[key];
				});
				that.$forceUpdate();
			},
			showModal(title, content) {
				this.modaltitle = title;
				this.modalcontent = content;
				this.modalshow = true;
			},
			modalconfirm() {
				var that = this
				that.modalshow = false
			},
			getList() {
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			IsStartClick(e) {
				this.IsStartShow = false
				this.FormData.Approve = e.id
			},
			addIsStart() {
				this.IsStartShow = true
			},
			scan() {
				let that = this;
				uni.scanCode({
					success: function(res) {
						console.log(JSON.stringify(res));
						that.jobid = res.result.replace(/^.*?\|/, '')
						that.Request.request('scanJobOrderForJY', {
							JobOrderID: that.jobid,
							FType: 'SJJ',
							EquipmentID: that.$store.state.GuyueModule.MesBasicInfo.warehouse
								.EquipmentID
						}, (ret) => {
							if (ret.status == 200) {
								if (res.data.ds.length > 0) {
									console.log(ret.data.ds);
									that.ProcedureList = JSON.parse(JSON.stringify(ret.data.ds))
									console.log(that.ProcedureList);
									let item = ret.data.ds.pop();
									item.InspectionID = 0
									item.JobOrderID = that.jobid
									var url
									let routes = getCurrentPages();
									let queryString =
										`item=${JSON.stringify(item)}&ds=${JSON.stringify(that.ProcedureList)}`;
									url = `/st_mes_zs/test/firstinspectionDetail?${queryString}`
									let obj = {
										path: 'manual',
										Name: '首件检详情',
										APPFunID: '首件检详情',
										length: routes.length + 1,
										url: url,
									}
									that.GonavigateTo(obj, url)
								} else {
									that.showModal('提示', '该派工单的线体和当前线体对应不上 不能扫码');

								}
							}
						})

					}
				});

			},
			planIDClick(item, index) {
				let that = this;
				that.hoveredIndex = index; // 设置当前hover的tr索引  
				var url
				let routes = getCurrentPages();
				item.JobOrderID = that.jobid
				let queryString = `item=${JSON.stringify(item)}&ds=${JSON.stringify(that.ProcedureList)}`;
				url = `/st_mes_zs/test/firstinspectionDetail?${queryString}`
				let obj = {
					path: 'manual',
					Name: '首件检详情',
					APPFunID: '首件检详情',
					length: routes.length + 1,
					url: url,
				}
				that.GonavigateTo(obj, url)
			},
			GonavigateTo(obj, url) {
				let that = this;
				var initData = JSON.parse(JSON.stringify(that.$store.state.GuyueModule.ModuleData_select));
				var parentData = initData[initData.length - 1]
				parentData.url = `/st_mes_zs/test/firstinspectionlist?Dispatchitem=${JSON.stringify(that.FormData)}`;
				that.$modifyParentData(parentData);
				this.$safeNavigate({
					targetUrl: url, // 目标页面路径
					payload: obj, // 要传递的数据
				});
			},
			async AddModuleData_select(res) {
				try {
					const backDelta = await this.GetModuleData_select(res);
				} catch (error) {
					console.error('返回操作失败:');
				}
			},
			addTime(name) {
				this.TimeName = name
				if (this.TimeName == '开始') {
					this.value1 = Number(getTimeStamp(this.FormData.FBDate))
				} else {
					this.value1 = Number(getTimeStamp(this.FormData.FEDate))
				}
				this.show = true
			},
			search() {
				this.$refs.popup.open('bottom')
			},
			submit() {
				let that = this;
				this.$refs.popup.close('bottom')
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			pickerConfirm(e) {
				let that = this;
				var time = getFormatDateTime(e.value)
				if (this.TimeName == '开始') {
					this.FormData.FBDate = time
				} else {
					this.FormData.FEDate = time
				}
				this.show = false
			},

			queryList(pageNo) {
				let that = this;
				that.FormData.PageIndex = pageNo
				try {
					that.Request.request('getInspectionList', this.FormData, (ret) => {
						console.log(ret);
						that.showMLoading = false
						if (ret.status == 200) {
							that.$refs.paging.complete(ret.data.ds);
							that.$forceUpdate()
						} else {
							that.showMLoading = false
						}
					}, (err) => {
						that.showMLoading = false
					})
				} catch (e) {
					console.log(e)
					that.showMLoading = false
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},
			changeLevel(list, data, row) {
				getchangeLevel(data)

			},
			clearHovered() {
				this.hoveredIndex = null; // 清除hover的tr索引  
			},

		}
	}
</script>

<style lang="scss" >
	@import "../css/index.css";
</style>